/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.tests.persistance;

import ar.com.wholeconsulting.pacc.model.Aparato;
import ar.com.wholeconsulting.pacc.model.Modelo;
import ar.com.wholeconsulting.pacc.model.Usuario;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author pable
 */
public class TestClass {

    public static void main(String[] args) throws Exception {
        //JPAUtil util = new JPAUtil();
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(
                "PACC");
        EntityManager manager = factory.createEntityManager();
        EntityTransaction transaction = manager.getTransaction();
        transaction.begin();

        Modelo m = new Modelo();
        Aparato ap = new Aparato();
        ap.setModelo(m);
        Usuario u = new Usuario();
        u.setNombre("miguel angel chagar1");
        u.addAparato(ap);
        ap.setUsuario(u);

        manager.persist(u);

        Query query =  manager.createNamedQuery("Usuario.findByNombre");

        query.setParameter("nombre", "miguel angel chagar");
        Usuario user = (Usuario) query.getSingleResult();
        System.out.println(user == u);
        Collection<Usuario> users = (Collection<Usuario>) manager.createNamedQuery("Usuario.findAll").getResultList();
        for(Usuario us : users){
            System.out.println(us.getNombre());
        }
//        Customer customer = new Customer();
//        customer.setSsn("111-11-1111");
//        customer.setFirstName("Reza");
//        customer.setLastName("Rahman");
//
//        Address address = new Address();
//        address.setStreet("155 Hillcrest Avenue");
//        address.setCity("Morrisville");
//        address.setState("Pennsylvania");
//        address.setZip("19067");
//
//        customer.setAddress(address);
//
//        Account account = new Account();
//
//        account.setAccountNumber("1111");
//        account.setAccountType(AccountType.CHECKING);
//        account.setBalance(100.00);
//        account.setCustomer(customer);
//
//        customer.addAccount(account);
//
//        manager.persist(customer);

//        Query query = manager.createNamedQuery("Customer.findBySsn");
//        query.setParameter("ssn", "111-11-1111");
//        Customer customer = (Customer) query.getSingleResult();
//
//        System.out.println(customer);
//
//        query = manager.createNamedQuery("Account.findByCustomer");
//        query.setParameter("customer", customer);
//        Account account = (Account) query.getSingleResult();
//
//        System.out.println(account);
//
//        AccountTransaction accountTransaction = new AccountTransaction();
//        accountTransaction.setTransactionType(TransactionType.DEPOSIT);
//        accountTransaction.setAccount(account);
//        accountTransaction.setAmount(100.00);
//        accountTransaction.setTransactionDate(new Date());
//
//        manager.persist(accountTransaction);
//
//        account.setBalance(account.getBalance() + 100.00);

        transaction.commit();

        manager.close();
        factory.close();
    }
}
